package com.nit.lab.utils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by shen on 2017/11/20.
 */
public class SessionContext {
    private static SessionContext instance;
    private HashMap<String, Object> mapSession;

    private SessionContext() {
        mapSession = new HashMap<String, Object>();
    }

    public static SessionContext getInstance() {

        if (instance == null) {
            instance = new SessionContext();
        }
        return instance;
    }

    public synchronized void addSession(HttpSession session) {
        if (session != null) {
            mapSession.put(session.getId(), session);
        }
    }

    public synchronized void delSession(HttpSession session) {
        if (session != null) {
            mapSession.remove(session.getId());
        }
    }

    public synchronized HttpSession getSession(String sessionId) {
        if (sessionId == null)
            return null;
        return (HttpSession) mapSession.get(sessionId);
    }
}
